<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<%@ include file="../taglibs.jsp"%>
<%@taglib prefix="staticResource" tagdir="/WEB-INF/tags/header"%>
<staticResource:staticResource></staticResource:staticResource>
<style type="text/css">
<title><fmt:message key="application.title"/></title>
b{color: #666;}
</style>
</head>
<body class="easyui-layout">
    <div data-options="region:'north',border:false" style="height:32px;background:#FFF;padding:0px">
	    <div class="navBox">
	     <table height="100%">
		    <tr>
			  <td><img src="${ctx }/static/images/home/icon0.png" /></td><td>${set.sysName}&gt;&gt; 系统管理&gt;&gt; 工况模板菜单列表</td>
			</tr>
		 </table>
	    </div>
    </div>  

    <div data-options="region:'center',title:''" style="overflow-x:hidden;padding: 5px;">  

       <div id="toolbar" style="padding:3px">  
	       	<div>
				<a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="add()">添加</a> 
				<a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="edit()">修改</a> 
				<a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="deleteAll()">删除</a>
				<a href="#" class="easyui-linkbutton" iconCls="icon-arrow-up" plain="true" onclick="arrow('up')">上移</a>
				<a href="#" class="easyui-linkbutton" iconCls="icon-arrow-down" plain="true" onclick="arrow('down')">下移</a>
			</div>
	        
	   </div>  
       <div style="float: left;width: 100%; height:auto;padding: 2px">

            <div style="float: left;width:100%;margin-top: 5px;">
                 
				<table id="dg" >  
				   
				</table>   
            </div>
       </div>
       
    </div>  
 
        <!--菜单信息  -->
       <div id="dlg" class="easyui-dialog" closed="true"   data-options="modal:true" style="width:450px;height:auto;">
		<form id="fm" method="post" >
		<input type="hidden" name="menuId">
		<input type="hidden" name="menuParentId" id="menuParentId">
		  <table id="table3" width="99%" height="210px">
		    <tr><td height="40px" colspan="2" align="center" valign="middle"><b>菜单信息</b></td>
		    <tr>
		      <td>菜单名称</td><td><input name="menuName" class="easyui-validatebox" style="width: 98%"></td>
		    </tr>
		     <tr>
		      <td>图标名称</td><td ><input name="menuIconName" class="easyui-validatebox" style="width: 98%"></td>
		    </tr>
		     <tr>
		      <td>图标路径</td><td ><input name="menuIconPath" class="easyui-validatebox" style="width: 98%"></td>
		    </tr>
		      <tr>
		      <td>访问链接</td><td ><input name="menuHref" class="easyui-validatebox" style="width: 98%"></td>
		    </tr>
		    <tr>
		      <td>描述</td><td ><textarea rows="3" cols="3" name="remark" style="width: 98%"></textarea> </td>
		    </tr>
		    <tr align="center">
	         <td height="30" colspan="2" align="center">
	         <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="save()">确定</a>
			 &nbsp;&nbsp;
		     <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')">取消</a>
	         </td>
	        </tr>
		  </table>
		</form>
	   </div>   
</body>


<!-- 实时数据加载 -->
<script type="text/javascript">
$(function(){
	// 获取浏览器窗口 
	var windowScreen = document.documentElement;
	var h=windowScreen.clientHeight;
	$('#dg').treegrid({  
		url : '${pageContext.request.contextPath}/system/menuModel/list',
		height : (h - 50),
		nowrap : true,
		autoRowHeight : true,
		striped : false,
		collapsible : false,
		fitColumns : true,
		remoteSort : false,
		pagination : false,//显示分页 
        singleSelect:true,
		idField : 'menuId',
		treeField:'menuName', 
		toolbar : $("#toolbar"),
        columns:[[  
            {field:'menuId',hidden:true},
            {field:'menuParentId',hidden:true},
            {field:'menuName',title:'菜单名称',width:10},
            {field:'menuIconName',title:'图标名称',width:10},
            {field:'menuIconPath',title:'图标路径',width:10},
            {field:'menuHref',title:'访问链接',width:10},
            {field:'menuOrder',title:'菜单顺序',width:10},
            {field:'remark',title:'描述',width:10}
        ]],
        onLoadSuccess: function () {
        	 $('#dg').treegrid('collapseAll');
        }
       
    });  
});

var url;
//add
function add() {
	var row = $('#dg').treegrid('getSelected');
	if (null==row) {
		$.messager.alert('提示', '请选择一条记录', 'info');
	}else{
		$.post('${ctx}/system/menuModel/isLeaf?menuParentId=' + row.menuParentId, function(
				flag) {
				if(flag){
					$.messager.alert('提示', '不能在该节点下增加子节点', 'info');
				}else{					
					$('#dlg').dialog('open').dialog('setTitle', '添加菜单');
					$('#fm').form('clear');
					$("#menuParentId").val(row.menuId);
					url = '${ctx}/system/menuModel/save';
				}
		}, 'json');
	}
	
}

//edit
function edit() {
	var row = $('#dg').treegrid('getSelected');
	if (null==row) {
		$.messager.alert('提示', '请选择一条记录', 'info');
	} else {
		$('#dlg').dialog('open').dialog('setTitle', '修改菜单');
		$('#fm').form('load', row);
		url = '${ctx}/system/menuModel/update';
	}
}
//save
function save() {
	$('#fm').form('submit', {
		url : url,
		onSubmit : function() {
			return $(this).form('validate');
		},
		success : function(response) {
			response = eval('(' + response + ')');
			$.messager.show({
				title : response.title,
				msg : response.msg,
				timeout : response.timeout,
				showType : response.showType
			});
			$('#dlg').dialog('close');
			$('#dg').treegrid('reload');
		}
	});
}
//deleteAll
function deleteAll() {
	var row = $('#dg').treegrid('getSelected');
	if (null==row) {
		$.messager.alert('提示', '请选择一条记录', 'info');
	}else if(row.status=='1'){
		$.messager.alert('提示', '不能删除启用状态的菜单', 'info');
	} else {
		$.post('${ctx}/system/menuModel/hasChildren?menuId=' + row.menuId, function(
				flag) {
			if(flag==true){
				$.messager.confirm('提示', '确认删除选中的记录吗?', function(r) {
					//获取treegrid选中行
					if (r) {
						$.post('${ctx}/system/menuModel/deleteAll?menuId=' + row.menuId, function(
								response) {
								$.messager.show({
									title : response.title,
									msg : response.msg,
									timeout : response.timeout,
									showType : response.showType
								});
								$('#dlg').dialog('close');
								$('#dg').treegrid('reload');
						}, 'json');
					}
				});
			}else{
				$.messager.alert('提示', '不能删除含有子菜单的父菜单', 'info');
			}
		
		}, 'json');
	}
}
// up and down
function arrow(flag){
	   var row = $('#dg').treegrid('getSelected');
	   if(!row){
			$.messager.alert('提示', '请选择一条记录', 'info');
	   }else{
			if(row.menuParentId==0){
				$.messager.alert('提示', '根节点不能移动！', 'info');
		   }else{
			   	if ("up" == flag) {
			    	   $.post("${ctx}/system/menuModel/getMinOrder/" + row.menuParentId, function (result) {
						   		//判断第一位
							   if(row.menuOrder==result){ 
									$.messager.alert('提示', '当前已处于第一位！', 'info');
						        }else{  
						        	//排序调整
						        	 $.post("${ctx}/system/menuModel/arrowUp/" + row.menuId+"/"+row.menuParentId, function (result) {
						        		 $('#dg').treegrid('reload');
						            });
						        }
		       	  			 });
	      			 } else if ("down" == flag) {
			    	   $.post("${ctx}/system/menuModel/getMaxOrder/" + row.menuParentId, function (result) {
					    	   //判断最后一位
					    	   if(row.menuOrder==result){  
									$.messager.alert('提示', '当前已处于最后一位！', 'info');
						        }else{  
						        	//排序调整
						        	 $.post("${ctx}/system/menuModel/arrowDown/" + row.menuId+"/"+row.menuParentId, function (result) {
						        		 $('#dg').treegrid('reload');
						        	 });
						        }
		        	  		 });
	      			  }
	   		}
	       }
}
//lock and open
function lock(flag){
	 var row = $('#dg').treegrid('getSelected');
	   if(!row){
			$.messager.alert('提示', '请选择一条记录', 'info');
	   }else if(row.status=='1' && row.status==flag){
			$.messager.alert('提示', '已经是启用状态', 'info');
	   }else if(row.status=='2' && row.status==flag){
			$.messager.alert('提示', '已经是禁用状态', 'info');
	   }else{
		   $.post("${ctx}/system/menu/lock/" + row.menuId+"/"+flag, function () {
				$('#dg').treegrid('reload');
   	  		 });
		   }
	   }
////////////////////////////////////////

function formatIsEnabled(key){
	<c:forEach items="${isEnabledList }" var="isEnabled">
	if('${enabled.code}'==key){		
		return  '${enabled.value}';
	}
	</c:forEach>
}
</script>
</html>
